package org.agilelang.generator.cakephp;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IFiles;
import org.agilelang.object.Application;

public class GFile extends Generator implements IFiles {

    public GFile(String path, Application app) {
        this.path = path;
        this.app = app;


    }

    @Override
    public void process() {
        //otherFile(this.app.name);
        //generateDatabase_php(this.app.host, this.app.login, this.app.password, this.app.database);
        //auth();
        directoriesStatic();
        filesStatic();
    }

    public void otherFile(String appname) {
        /*this.createDirectory(path + "\\app\\View\\Mains");
        String file = readStreamFiles("ctp_index_mains.tpl");
        createfile("app\\View\\Mains\\index.ctp", path, file);
        //----------------------------------------------------------------------
        file = readStreamFiles("php_mains.tpl");
        file = file.replace("//AppName//", appname);
        file = file.replace("//Title//", "Inicio");
        createfile("app\\Controller\\MainsController.php", path, file);
        //----------------------------------------------------------------------

        //UtilFile.copyfile("..\\fpdf17_php.zip", path + "/fpdf17.zip");
        //UtilFile.unzip(path + "/fpdf17.zip");

        //UtilFile.copyfile("data/openpyxl.zip", path + "/openpyxl.zip");
        //UtilFile.unzip(path + "/openpyxl.zip");
        //----------------------------------------------------------------------
        file = readStreamFiles("ctp_layout.tpl");
        file = file.replace("//AppName//", appname);
        file = file.replace("//menu_main//", "");
        file = file.replace("//menu_tab//", "");
        createfile("app\\View\\Layouts\\default.ctp", path, file);
        fileRelocate("pdf_layout.tpl", path + "/app/View/Layouts/pdf.ctp");
        fileRelocate("PDF.php", path + "/vendors/PDF.php");

        //----------------------------------------------------------------------
        file = readStreamFiles("routes.php");
        createfile("app\\Config\\routes.php", path, file);
        //----------------------------------------------------------------------
        file = readStreamFiles("style.css");
        this.createDirectory(path + "/app/webroot/css/smoothness/");
        this.createDirectory(path + "/app/webroot/css/smoothness/images/");
        createfile("app\\webroot\\css\\style.css", path, file);
        fileRelocate("jquery-ui-1.9.1.custom.min.css", path + "/app/webroot/css/smoothness/jquery-ui-1.9.1.custom.min.css");

        fileRelocate("jquery-1.8.2.js", path + "/app/webroot/js/jquery-1.8.2.js");
        fileRelocate("jquery-ui-1.9.1.custom.min.js", path + "/app/webroot/js/jquery-ui-1.9.1.custom.min.js");

        //----------------------------------------------------------------------
        this.createDirectory(path + "\\app\\webroot\\images");
        createImageIcon("add.png", path + "\\app\\webroot\\images\\add.png");
        createImageIcon("calendar.png", path + "\\app\\webroot\\images\\calendar.png");
        createImageIcon("delete.png", path + "\\app\\webroot\\images\\delete.png");
        createImageIcon("edit.png", path + "\\app\\webroot\\images\\edit.png");
        createImageIcon("read.png", path + "\\app\\webroot\\images\\read.png");
        createImageIcon("ui-bg_flat_0_aaaaaa_40x100.png", path + "/app/webroot/css/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png");
        createImageIcon("ui-bg_flat_75_ffffff_40x100.png", path + "/app/webroot/css/smoothness/images/ui-bg_flat_75_ffffff_40x100.png");
        createImageIcon("ui-bg_glass_55_fbf9ee_1x400.png", path + "/app/webroot/css/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png");
        createImageIcon("ui-bg_glass_65_ffffff_1x400.png", path + "/app/webroot/css/smoothness/images/ui-bg_glass_65_ffffff_1x400.png");
        createImageIcon("ui-bg_glass_75_dadada_1x400.png", path + "/app/webroot/css/smoothness/images/ui-bg_glass_75_dadada_1x400.png");
        createImageIcon("ui-bg_glass_75_e6e6e6_1x400.png", path + "/app/webroot/css/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png");
        createImageIcon("ui-bg_glass_95_fef1ec_1x400.png", path + "/app/webroot/css/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png");
        createImageIcon("ui-bg_highlight-soft_75_cccccc_1x100.png", path + "/app/webroot/css/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png");
        createImageIcon("ui-icons_222222_256x240.png", path + "/app/webroot/css/smoothness/images/ui-icons_222222_256x240.png");
        createImageIcon("ui-icons_2e83ff_256x240.png", path + "/app/webroot/css/smoothness/images/ui-icons_2e83ff_256x240.png");
        createImageIcon("ui-icons_454545_256x240.png", path + "/app/webroot/css/smoothness/images/ui-icons_454545_256x240.png");
        createImageIcon("ui-icons_888888_256x240.png", path + "/app/webroot/css/smoothness/images/ui-icons_888888_256x240.png");
        createImageIcon("ui-icons_cd0a0a_256x240.png", path + "/app/webroot/css/smoothness/images/ui-icons_cd0a0a_256x240.png");
*/
        //auth();
    }

    public void generateDatabase_php(String host, String login, String password, String database) {
        /*String file = readStreamFiles("database.php.tpl");
        file = file.replace("//host//", host);
        file = file.replace("//login//", login);
        file = file.replace("//password//", password);
        file = file.replace("//database//", database);
        createfile("app\\Config\\database.php", path, file);*/
    }

    public void auth() {
/*
        fileRelocate("jquery.checktree.css", path + "/app/webroot/css/jquery.checktree.css");
        fileRelocate("jquery.checktree_yctin.min.js", path + "/app/webroot/js/jquery.checktree_yctin.min.js");
        fileRelocate("resourcetree.js", path + "/app/webroot/js/resourcetree.js");

        this.createDirectory(path + "\\app\\webroot\\css\\images");
        createImageIcon("check0.gif", path + "/app/webroot/css/images/check0.gif");
        createImageIcon("check1.gif", path + "/app/webroot/css/images/check1.gif");
        createImageIcon("check2.gif", path + "/app/webroot/css/images/check2.gif");
        createImageIcon("collapsed.gif", path + "/app/webroot/css/images/collapsed.gif");
        createImageIcon("expanded.gif", path + "/app/webroot/css/images/expanded.gif");


        fileRelocate("bootstrap.php", path + "/app/Config/bootstrap.php");

        fileRelocate("auth_menu.php", path + "/app/Model/Menu.php");
        fileRelocate("auth_permission.php", path + "/app/Model/Permission.php");
        fileRelocate("auth_resource.php", path + "/app/Model/Resource.php");
        fileRelocate("auth_resourcegroup.php", path + "/app/Model/Resourcegroup.php");
        fileRelocate("auth_user.php", path + "/app/Model/User.php");
        fileRelocate("auth_usergroup.php", path + "/app/Model/Role.php");


        fileRelocate("AppController.php", path + "/app/Controller/AppController.php");

        fileRelocate("auth_menus_controller.php", path + "/app/Controller/MenusController.php");
        fileRelocate("auth_resourcegroups_controller.php", path + "/app/Controller/ResourcegroupsController.php");
        fileRelocate("auth_resources_controller.php", path + "/app/Controller/ResourcesController.php");
        fileRelocate("auth_usergroups_controller.php", path + "/app/Controller/RolesController.php");
        fileRelocate("auth_users_controller.php", path + "/app/Controller/UsersController.php");

        this.createDirectory(path + "/app/sql/");
        this.createDirectory(path + "/app/view/Menus/");
        this.createDirectory(path + "/app/view/Resources/");
        this.createDirectory(path + "/app/view/Resourcegroups/");
        this.createDirectory(path + "/app/view/Users/");
        this.createDirectory(path + "/app/view/Roles/");

        fileRelocate("auth_menus_add.ctp", path + "/app/view/Menus/add.ctp");
        fileRelocate("auth_menus_edit.ctp", path + "/app/view/Menus/edit.ctp");
        fileRelocate("auth_menus_index.ctp", path + "/app/view/Menus/index.ctp");
        fileRelocate("auth_menus_view.ctp", path + "/app/view/Menus/view.ctp");


        fileRelocate("auth_resourcegroups_add.ctp", path + "/app/view/Resourcegroups/add.ctp");
        fileRelocate("auth_resourcegroups_edit.ctp", path + "/app/view/Resourcegroups/edit.ctp");
        fileRelocate("auth_resourcegroups_index.ctp", path + "/app/view/Resourcegroups/index.ctp");
        fileRelocate("auth_resourcegroups_view.ctp", path + "/app/view/authresourcegroups/view.ctp");

        fileRelocate("auth_resources_add.ctp", path + "/app/view/Resources/add.ctp");
        fileRelocate("auth_resources_edit.ctp", path + "/app/view/Resources/edit.ctp");
        fileRelocate("auth_resources_index.ctp", path + "/app/view/Resources/index.ctp");
        fileRelocate("auth_resources_view.ctp", path + "/app/view/Resources/view.ctp");

        fileRelocate("auth_usergroups_add.ctp", path + "/app/view/Roles/add.ctp");
        fileRelocate("auth_usergroups_edit.ctp", path + "/app/view/Roles/edit.ctp");
        fileRelocate("auth_usergroups_index.ctp", path + "/app/view/Roles/index.ctp");
        fileRelocate("auth_usergroups_view.ctp", path + "/app/view/Roles/view.ctp");

        fileRelocate("auth_users_add.ctp", path + "/app/view/Users/add.ctp");
        fileRelocate("auth_users_edit.ctp", path + "/app/view/Users/edit.ctp");
        fileRelocate("auth_users_index.ctp", path + "/app/view/Users/index.ctp");
        fileRelocate("auth_users_login.ctp", path + "/app/view/Users/login.ctp");
        fileRelocate("auth_users_view.ctp", path + "/app/view/Users/view.ctp");

        fileRelocate("auth_login.ctp", path + "/app/view/Layouts/login.ctp");

        this.createDirectory(path + "/sql/");
        fileRelocate("auth.sql", path + "/sql/auth.sql");
*/

    }

    public void directoriesStatic() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/cakephp/bootstrap/files/_directories.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticDirectories staticDirectories = null;
        try {
            jc = JAXBContext.newInstance(StaticDirectories.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticDirectories = (StaticDirectories) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticDirectory> directories = staticDirectories.directories;
        for (StaticDirectory directory : directories) {
            this.createDirectory(path + directory.path);
            System.out.println(directory.path);
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }

    public void filesStatic() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/cakephp/bootstrap/files/_files.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticFiles staticFiles = null;
        try {
            jc = JAXBContext.newInstance(StaticFiles.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticFiles = (StaticFiles) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticFile> files = staticFiles.files;
        for (StaticFile file : files) {
            System.out.println(file.origin);
            if (file.type.equals("file")) {
                fileRelocateFile("cakephp", file.origin, path + file.destination);
            }
            if (file.type.equals("imagen")) {
                createImageIcon("cakephp", file.origin, path + file.destination);
            }
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }
}
